package com.teacherprofile.manage.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacherprofile.common.annotation.DataScope;
import com.teacherprofile.common.constant.AcademicPapersConstants;
import com.teacherprofile.common.constant.BusinessConstants;
import com.teacherprofile.common.core.domain.entity.SysUser;
import com.teacherprofile.common.enums.CheckStatus;
import com.teacherprofile.common.exception.ServiceException;
import com.teacherprofile.framework.manager.AsyncManager;
import com.teacherprofile.framework.manager.factory.AsyncFactory;
import com.teacherprofile.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.teacherprofile.manage.mapper.EducationalBackgroundMapper;
import com.teacherprofile.common.domain.EducationalBackground;
import com.teacherprofile.manage.service.IEducationalBackgroundService;
import org.springframework.util.CollectionUtils;

/**
 * 教育背景Service业务层处理
 *
 * @author qvtu
 * @date 2025-05-17
 */
@Service
public class EducationalBackgroundServiceImpl extends ServiceImpl<EducationalBackgroundMapper, EducationalBackground> implements IEducationalBackgroundService {
    @Autowired
    private EducationalBackgroundMapper educationalBackgroundMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询教育背景
     *
     * @param id 教育背景主键
     * @return 教育背景
     */
    @Override
    public EducationalBackground selectEducationalBackgroundById(Long id) {
        return educationalBackgroundMapper.selectEducationalBackgroundById(id);
    }

    /**
     * 查询教育背景列表
     *
     * @param educationalBackground 教育背景
     * @return 教育背景
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EducationalBackground> selectEducationalBackgroundList(EducationalBackground educationalBackground) {
        return educationalBackgroundMapper.selectEducationalBackgroundList(educationalBackground);
    }

    /**
     * 新增教育背景
     *
     * @param educationalBackground 教育背景
     * @return 结果
     */
    @Override
    public boolean insertEducationalBackground(EducationalBackground educationalBackground) {
        String userName = educationalBackground.getUserName();
        if (ObjectUtils.isEmpty(userName)) {
            throw new RuntimeException("教职工号不能为空");
        }
        // 根据教职工号查询用户
        SysUser sysUser = userService.selectUserByUserName(userName);
        if (ObjectUtils.isEmpty(sysUser)) {
            // 用户不存在
            throw new RuntimeException("教职工号不存在");
        }
        // 用户存在，设置用户的userId与部门id，保存
        educationalBackground.setUserId(sysUser.getUserId());
        educationalBackground.setDeptId(sysUser.getDeptId());
        return save(educationalBackground);
    }

    /**
     * 修改教育背景
     *
     * @param educationalBackground 教育背景
     * @return 结果
     */
    @Override
    public boolean updateEducationalBackground(EducationalBackground educationalBackground) {
        return updateById(educationalBackground);
    }

    /**
     * 批量删除教育背景
     *
     * @param ids 需要删除的教育背景主键
     * @return 结果
     */
    @Override
    public int deleteEducationalBackgroundByIds(Long[] ids) {
        return educationalBackgroundMapper.deleteEducationalBackgroundByIds(ids);
    }

    /**
     * 删除教育背景信息
     *
     * @param id 教育背景主键
     * @return 结果
     */
    @Override
    public int deleteEducationalBackgroundById(Long id) {
        return educationalBackgroundMapper.deleteEducationalBackgroundById(id);
    }

//    /**
//     * 导入数据
//     *
//     * @param educationalBackground 工作荣誉
//     * @return
//     */
//    @Override
//    public String importData(List<EducationalBackground> educationalBackground) {
//        if (CollectionUtils.isEmpty(educationalBackground)) {
//            throw new ServiceException("导入数据不能为空！");
//        }
//        // 查询导入的教职工号去重集合
//        Set<String> userNameList = educationalBackground.stream().map(EducationalBackground::getUserName).collect(Collectors.toSet());
//        // 根据去重集合查询对应的用户
//        List<SysUser> userList = userService.selectUserByUserNames(userNameList);
//        if (ObjectUtils.isEmpty(userList)) {
//            throw new ServiceException("导入数据中教职工号不存在！");
//        }
//        // 将列表转成Map，key为教职工号，value为SysUser对象
//        Map<String, SysUser> userMap = userList.stream().collect(Collectors.toMap(SysUser::getUserName, item -> item));
//        for (EducationalBackground item : educationalBackground) {
//            // 填充教育背景的userId与deptId
//            SysUser sysUser = userMap.get(item.getUserName());
//            item.setUserId(sysUser.getUserId());
//            item.setDeptId(sysUser.getDeptId());
//        }
//        return saveBatch(educationalBackground) ? "导入成功" : "导入失败";
//    }

//    /**
//     * 审批
//     *
//     * @param educationalBackground 工作荣誉
//     * @return
//     */
//    @Override
//    public boolean check(EducationalBackground educationalBackground) {
//        if (ObjectUtils.isNull(educationalBackground)) { // 非法参数
//            throw new ServiceException(BusinessConstants.BUSINESS_ERROR);
//        }
//        if (educationalBackground.getCheckStatus().equals(CheckStatus.PASS.getCode())) {
//            // 审核通过
//            // TODO 发送mq消息，存储数据
//            //rabbitMqHelper.send(MqConstants.Exchange.SOFTWARE_COPYWRITE_EXCHANGE,
//            //                    MqConstants.Key.SOFTWARE_COPYWRITE_PASS_KEY, softwareCopyrights);
//
//            // 通知用户，审核通过
//            // TODO 修改常量
//            AsyncManager.me().execute(AsyncFactory.sendPassMsgToOne(educationalBackground.getUserName(), AcademicPapersConstants.CHECKED_PASS));
//        } else {
//            // 审核不通过
//            // TODO 发送mq消息，存储数据
//            // 通知用户，审核不通过
//            // TODO 修改常量
//            AsyncManager.me().execute(AsyncFactory.sendRejectMsgToOne(educationalBackground.getUserName(), AcademicPapersConstants.CHECKED_REJECT));
//        }
//        // 删除此条数据
//
//        return updateById(educationalBackground);
//    }
}
